package cn.stylefeng.guns.modular.hp.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.hp.constant.ArticleEnum;
import cn.stylefeng.guns.modular.hp.domain.ArticleInfoDO;
import cn.stylefeng.guns.modular.hp.domain.ModularDO;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.MenuInfo;
import cn.stylefeng.guns.modular.hp.model.PageContentInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IMenuInfoService;
import cn.stylefeng.guns.modular.hp.service.IPageContentInfoService;
import cn.stylefeng.guns.modular.hp.warpper.ArticleInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 通用列表信息控制器
 *
 * @author xzqiang
 * @date 2018-05-31
 */
@Controller
@RequestMapping("/hp/currency")
public class CurrencyInfoController extends BaseController {

    private String PREFIX = "/hp/currency/";
    
    @Autowired
    private HpProperties hpProperties;

    @Autowired
    private IArticleInfoService articleInfoService;
    
    @Autowired
    private IPageContentInfoService pageContentInfoService;

	@Autowired
	private IMenuInfoService menuInfoService;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
    	String htmlPath = "currency.html";
        return PREFIX + htmlPath;
    }

    /**
     * 获取通用模块列表
     */
    @RequestMapping("/getModularList")
	@ResponseBody
    public Object getModularList() {
        List<MenuInfo> allMenuInfoList = this.menuInfoService.getAllModularInfolist();

        Map<String, ModularDO> modularDOMap = getDefaultModular();

        for(MenuInfo info : allMenuInfoList){
            if(!modularDOMap.containsKey(info.getCode())){
                modularDOMap.put(info.getCode(), new ModularDO(info.getCode(), info.getName()));
            }
        }
        Collection<ModularDO> modularDOList = modularDOMap.values();
        //Collection转List
        List<ModularDO> resultList = new ArrayList<ModularDO>(modularDOList);
        //排序
        Collections.sort(resultList);

        return resultList;
    }

    /**
     * 跳转到修改信息详情
     */
    @RequestMapping("/info_update/{infoId}")
    public String infoUpdate(@PathVariable String infoId, Model model) {
        ArticleInfo info = this.articleInfoService.selectById(infoId);
        
        PageContentInfo pageContent = this.pageContentInfoService.selectById(info.getPageContentId());
        
        ArticleInfoDO articleInfoDO = new ArticleInfoDO();
        articleInfoDO.setId(info.getId());
        articleInfoDO.setModular(info.getModular());
        articleInfoDO.setTitle(info.getTitle());
        articleInfoDO.setContent(pageContent.getContent());
        articleInfoDO.setPri(info.getPri());
        articleInfoDO.setCreatedTime(DateUtil.format(info.getCreatedTime(), "yyyy-MM-dd HH:mm:ss"));
        
        model.addAttribute("info", articleInfoDO);
        LogObjectHolder.me().set(articleInfoDO);

        String htmlPath = "currency_edit.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到添加通知
     */
    @RequestMapping("/info_add")
    public String infoAdd() {
        String htmlPath = "currency_add.html";
        return PREFIX + htmlPath;
    }

    /**
     * 获取模块内容列表
     */
    @RequestMapping(value = "/list/{modularType}")
    @ResponseBody
    public Object list(@PathVariable String modularType, String condition) {
        List<Map<String, Object>> list = this.articleInfoService.list(modularType, condition);
        return super.warpObject(new ArticleInfoWrapper(list));
    }

    /**
     * 新增信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增通知")
    public Object add(ArticleInfoDO articleInfoDO) {
        if (ToolUtil.isOneEmpty(articleInfoDO, articleInfoDO.getTitle(), articleInfoDO.getContent(), articleInfoDO.getModular())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        //新增页面内容
        PageContentInfo pageContent = new PageContentInfo();
        pageContent.setContent(articleInfoDO.getContent());
        pageContent.setCreater(ShiroKit.getUser().getId());
        pageContent.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(), "yyyy-MM-dd HH:mm:ss"));
        pageContent.insert();
        
        //新增公告内容
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setModular(articleInfoDO.getModular());
        articleInfo.setTitle(articleInfoDO.getTitle());
        articleInfo.setLinkUrl(pageContent.getId());
        articleInfo.setPageContentId(pageContent.getId());
        articleInfo.setPri(articleInfoDO.getPri());
        articleInfo.setCreater(ShiroKit.getUser().getId());
        articleInfo.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
        articleInfo.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除通知
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知", key = "infoId")
    public Object delete(@RequestParam String infoId) {

        this.articleInfoService.deleteById(infoId);

        return SUCCESS_TIP;
    }

    /**
     * 修改信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改通知", key = "id")
    public Object update(ArticleInfoDO articleInfoDO) {
        if (ToolUtil.isOneEmpty(articleInfoDO, articleInfoDO.getId(), articleInfoDO.getTitle(), articleInfoDO.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        ArticleInfo oldArticleInfo = this.articleInfoService.selectById(articleInfoDO.getId());
        PageContentInfo oldPageContent = this.pageContentInfoService.selectById(oldArticleInfo.getPageContentId());
        
        
        oldPageContent.setContent(articleInfoDO.getContent());
    	oldPageContent.updateById();
        
    	oldArticleInfo.setTitle(articleInfoDO.getTitle());
    	oldArticleInfo.setPageContentId(oldPageContent.getId());
    	oldArticleInfo.setLinkUrl(oldPageContent.getId());
    	oldArticleInfo.setPri(articleInfoDO.getPri());
    	oldArticleInfo.updateById();
        return SUCCESS_TIP;
    }


    /**
     * 获取默认模块
     * @return
     */
    private Map<String, ModularDO> getDefaultModular(){
        Map<String, ModularDO> modularDO = new HashMap<>();
        modularDO.put(ArticleEnum.DEPARTMENT, new ModularDO(ArticleEnum.DEPARTMENT, ArticleEnum.DEPARTMENT_NAME));
        modularDO.put(ArticleEnum.SRT, new ModularDO(ArticleEnum.SRT, ArticleEnum.SRT_NAME));
        modularDO.put(ArticleEnum.HE, new ModularDO(ArticleEnum.HE, ArticleEnum.HE_NAME));
        modularDO.put(ArticleEnum.KCM, new ModularDO(ArticleEnum.KCM, ArticleEnum.KCM_NAME));
        modularDO.put(ArticleEnum.NG, new ModularDO(ArticleEnum.NG, ArticleEnum.NG_NAME));
        modularDO.put(ArticleEnum.PBG, new ModularDO(ArticleEnum.PBG, ArticleEnum.PBG_NAME));
        modularDO.put(ArticleEnum.MIP, new ModularDO(ArticleEnum.MIP, ArticleEnum.MIP_NAME));
        modularDO.put(ArticleEnum.PA, new ModularDO(ArticleEnum.PA, ArticleEnum.PA_NAME));
        modularDO.put(ArticleEnum.PR, new ModularDO(ArticleEnum.PR, ArticleEnum.PR_NAME));
        return modularDO;
    }
}
